/**
 * GWT Kad : Kademlia implementation for GWT
 * Copyright (C) 2012  Pierrick HYMBERT, Paul-André MISTRAL, Florian ALEXANDRE
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.kad.gwt.web.client.transport.http;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.kad.gwt.web.client.mvp.AbstractHasHandlers;
import org.kad.gwt.web.client.transport.http.applet.GWTKadAppletClient;
import org.kad.gwt.web.client.transport.http.listeners.GWTKadAppletErrorListener;
import org.kad.gwt.web.client.transport.http.listeners.GWTKadAppletMessageListener;
import org.kad.gwt.web.client.transport.http.mvp.ready.HasHttpServerReadyHandlers;
import org.kad.gwt.web.client.transport.http.mvp.ready.HttpServerReadyEvent;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.RepeatingCommand;
import com.google.gwt.event.shared.EventBus;

/**
 * Expose methods to communicate with the applet http server.
 * 
 * @author Pierrick HYMBERT &lt;pierrick [dot] hymbert [at] gmail [dot] com&gt;
 */
@Singleton
public class GWTKadHttpAppletServerService extends AbstractHasHandlers implements HasHttpServerReadyHandlers {
    /**
     * The gwt kad applet gwt bridge.
     */
    private final GWTKadAppletClient          gwtKadAppletClient;
    /**
     * The gwt kad applet message listener.
     */
    private final GWTKadAppletMessageListener gwtKadAppletMessageListener;
    /**
     * The gwt kad applet gwt bridge.
     */
    private final GWTKadAppletErrorListener   gwtKadAppletErrorListener;

    /**
     * Constructor.
     * 
     * @param eventBusIn
     *            The event bus
     * @param gwtKadAppletClientIn
     *            {@link #gwtKadAppletClient}
     * @param gwtKadAppletMessageListenerIn
     *            {@link #gwtKadAppletMessageListener}
     * @param gwtKadAppletErrorListenerIn
     *            {@link #gwtKadAppletErrorListener}
     */
    @Inject
    public GWTKadHttpAppletServerService(EventBus eventBusIn, GWTKadAppletClient gwtKadAppletClientIn,
            GWTKadAppletMessageListener gwtKadAppletMessageListenerIn,
            GWTKadAppletErrorListener gwtKadAppletErrorListenerIn) {
        super(eventBusIn);
        this.gwtKadAppletClient = gwtKadAppletClientIn;
        this.gwtKadAppletMessageListener = gwtKadAppletMessageListenerIn;
        this.gwtKadAppletErrorListener = gwtKadAppletErrorListenerIn;
    }
    /**
     * Start the http server (using the applet). Fire {@link HttpServerReadyEvent} when the server is ready.
     */
    public void start() {
        Scheduler.get().scheduleFixedPeriod(new RepeatingCommand() {
            @Override
            public boolean execute() {
                if (getGwtKadAppletClient().isReady()) {
                    registerListeners();
                    HttpServerReadyEvent.fire(GWTKadHttpAppletServerService.this);
                    return false;
                }
                return true;
            }
        }, 200);
    }
    /**
     * @return The server listen port.
     */
    public int getServerListenPort() {
       return gwtKadAppletClient.getListenPort();
    }
    /**
     * Register applet listeners.
     */
    void registerListeners() {
        gwtKadAppletClient.addErrorListener(gwtKadAppletErrorListener);
        gwtKadAppletClient.addMessageListener(gwtKadAppletMessageListener);
    }
    /**
     * @return {@link #gwtKadAppletClient}
     */
    GWTKadAppletClient getGwtKadAppletClient() {
        return gwtKadAppletClient;
    }
}
